package com.example.wangfanwangluo.wangfanboss;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import static com.example.wangfanwangluo.wangfanboss.R.id.daoche_underline;
import static com.example.wangfanwangluo.wangfanboss.R.id.fache_back;
import static com.example.wangfanwangluo.wangfanboss.R.id.fache_daoche;
import static com.example.wangfanwangluo.wangfanboss.R.id.fache_fache;
import static com.example.wangfanwangluo.wangfanboss.R.id.fache_title;
import static com.example.wangfanwangluo.wangfanboss.R.id.fache_underline;

/**
 * Created by wanghouyi on 2017/11/28.
 */

public class FacheActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView fache_back_view;  //返回按钮
    private RelativeLayout fache_fache_layout;  //
    private RelativeLayout fache_daoche_layout;
    private Fragment facheFm; //发车fragment
    private Fragment daocheFm;  //到车fragment
    private TextView fache_underline_view;  //发车下的绿色线条
    private TextView daoche_underline_view; //到车下的绿色线条
    private TextView fache_title_view; //title

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fache);
        //隐藏标题栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ActionBar actionbar = getSupportActionBar();
        if (actionbar != null) {
            actionbar.hide();
        }
        initView();
        initEvents();

        Bundle bundle = getIntent().getExtras();
        //根据type值判断进入发车界面/到车界面
        String type = bundle.getString("type");
        if (type.equals("fache")) {
            select(0); //发车界面
            fache_title_view.setText("发车清单");
        } else if (type.equals("daoche")) {
            fache_title_view.setText("到车清单");
            select(1); //到车界面
        }
    }

    private void initView(){
        fache_back_view = (TextView) findViewById(fache_back);
        fache_fache_layout = (RelativeLayout) findViewById(fache_fache);
        fache_daoche_layout = (RelativeLayout) findViewById(fache_daoche);
        fache_underline_view = (TextView) findViewById(fache_underline);
        daoche_underline_view = (TextView) findViewById(daoche_underline);
        fache_title_view = (TextView) findViewById(fache_title);
    }

    private void initEvents(){
        fache_fache_layout.setOnClickListener(this);
        fache_daoche_layout.setOnClickListener(this);
        fache_back_view.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case fache_fache://发车
                select(0); //切换fragment
                break;
            case fache_daoche://到车
                select(1);
                break;
            case fache_back://返回首页
                finish();
                break;
            default:
                break;
        }
    }
    //隐藏fragment
    private void hideFragment(FragmentTransaction transaction){
        if (facheFm != null){
            //如果当前有运单详情fragment时，隐藏
            transaction.hide(facheFm);
        }

        if (daocheFm != null){
            //如果当前有运单跟踪fragment时，隐藏
            transaction.hide(daocheFm);
        }
        fache_underline_view.setVisibility(View.GONE);
        daoche_underline_view.setVisibility(View.GONE);
    }

    private void select(int i){
        //获取fragmentManger
        FragmentManager fragmentManager = getSupportFragmentManager();
        //开启一个事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i){
            case 0:
                if (facheFm == null){
                    //如果当前没有运单详情fragmen，则创建fragment
                    facheFm = new FacheFragment();
                    //将运单详情fragment添加到fragmentTransaction事务中
                    fragmentTransaction.add(R.id.fache_fragment, facheFm);
                } else {
                    fragmentTransaction.show(facheFm);
                }
                fache_underline_view.setVisibility(View.VISIBLE);
                daoche_underline_view.setVisibility(View.GONE);
                break;
            case 1:
                if (daocheFm == null){
                    daocheFm = new DaocheFragment();
                    fragmentTransaction.add(R.id.fache_fragment, daocheFm);
                } else {
                    fragmentTransaction.show(daocheFm);
                }
                fache_underline_view.setVisibility(View.GONE);
                daoche_underline_view.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
        //提交事务，用commit来完成
        fragmentTransaction.commit();
    }
}
